extends Node2D

signal KH(jd: float)

var DR:Area2D

func _process(delta: float) -> void:
	if DR != null :
		XZ_PT(DR)


func _on_area_2d_area_entered(area: Area2D) -> void:
	if area.is_in_group("敌人"):
		KH.emit(area)
		$KHTimer.start()
		print("检测到了")
		print(area)
		XZ_PT(area)

#开火角度
func KH_JD(area: Area2D) -> float:
	var other_global_pos = area.global_position
	# 获取 Area2D 自己的 Global 位置
	var my_global_pos = self.global_position
	var relative_position = other_global_pos - my_global_pos
	return rad_to_deg(relative_position.angle())


func XZ_PT(area: Area2D) -> void:
	$PT.rotation_degrees = KH_JD(area)
	DR = area

func go_up():
	$DP.rotation = -PI/2

func go_left():
	$DP.rotation = 0

func go_right():
	$DP.rotation = PI

func go_down():
	$DP.rotation = PI/2


func _on_area_2d_area_exited(area: Area2D) -> void:
	if area.is_in_group("敌人"):
		print("停止定时器")
		$KHTimer.stop()
		DR = null


func Ka_Huo():
	if DR :
		print("有敌人开火")
		KH.emit(KH_JD(DR))
		



func _on_kh_timer_timeout() -> void:
	if DR :
		Ka_Huo()
	
